/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.domain.IsfdSectionAlgorithm;
import com.comac.ins.isfd.domain.bo.IsfdSectionAlgorithmBo;
import com.comac.ins.isfd.domain.vo.IsfdSectionAlgorithmVo;
import com.comac.ins.isfd.mapper.IsfdSectionAlgorithmMapper;
import com.comac.ins.isfd.service.IIsfdSectionAlgorithmService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 型材算法Service业务层处理
 *
 * @author hxloongs
 * @date 2024-07-31
 */
@RequiredArgsConstructor
@Service
public class IsfdSectionAlgorithmServiceImpl implements IIsfdSectionAlgorithmService {

    @Autowired
    private IsfdSectionAlgorithmMapper baseMapper;

    /**
     * 查询型材算法
     *
     * @param id 主键
     * @return 型材算法
     */
    @Override
    public IsfdSectionAlgorithmVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询型材算法列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 型材算法分页列表
     */
    @Override
    public TableDataInfo<IsfdSectionAlgorithmVo> queryPageList(IsfdSectionAlgorithmBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IsfdSectionAlgorithm> lqw = buildQueryWrapper(bo);
        Page<IsfdSectionAlgorithmVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的型材算法列表
     *
     * @param bo 查询条件
     * @return 型材算法列表
     */
    @Override
    public List<IsfdSectionAlgorithmVo> queryList(IsfdSectionAlgorithmBo bo) {
        LambdaQueryWrapper<IsfdSectionAlgorithm> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdSectionAlgorithm> buildQueryWrapper(IsfdSectionAlgorithmBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IsfdSectionAlgorithm> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCustomAlgorithm()), IsfdSectionAlgorithm::getCustomAlgorithm, bo.getCustomAlgorithm());
        return lqw;
    }

    /**
     * 新增型材算法
     *
     * @param bo 型材算法
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(IsfdSectionAlgorithmBo bo) {
        IsfdSectionAlgorithm add = MapstructUtils.convert(bo, IsfdSectionAlgorithm.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改型材算法
     *
     * @param bo 型材算法
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(IsfdSectionAlgorithmBo bo) {
        IsfdSectionAlgorithm update = MapstructUtils.convert(bo, IsfdSectionAlgorithm.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IsfdSectionAlgorithm entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除型材算法信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
